|
1
|
|
|
import React from 'react'; |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
class Login extends React.Component { |
|
5
|
|
|
|
|
6
|
|
|
constructor(props) { |
|
7
|
|
|
super(props); |
|
8
|
|
|
this.state = { |
|
9
|
|
|
email: "", |
|
10
|
|
|
password: "" |
|
11
|
|
|
} |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
componentDidMount() { |
|
15
|
|
|
if(localStorage.getItem('user')){ |
|
16
|
|
|
localStorage.setItem('user', ""); |
|
17
|
|
|
window.location.replace("/"); |
|
18
|
|
|
} |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
handleOnChange = (event) => { |
|
22
|
|
|
let name = event.target.name; |
|
23
|
|
|
let value = event.target.value; |
|
24
|
|
|
this.setState({[name]: value}); |
|
25
|
|
|
}; |
|
26
|
|
|
|
|
27
|
|
|
handleSubmit = (event) => { |
|
28
|
|
|
event.preventDefault(); |
|
29
|
|
|
fetch("http://localhost:8333/auth/login", { |
|
30
|
|
|
method: "POST", |
|
31
|
|
|
headers: { |
|
32
|
|
|
'Accept': 'application/json', |
|
33
|
|
|
'Content-Type': 'application/json', |
|
34
|
|
|
}, |
|
35
|
|
|
body: JSON.stringify({ |
|
36
|
|
|
email: this.state.email, |
|
37
|
|
|
password: this.state.password |
|
38
|
|
|
}) |
|
39
|
|
|
}).then((res) => { |
|
40
|
|
|
return res.json(); |
|
41
|
|
|
}).then((res)=>{ |
|
42
|
|
|
console.log(res); |
|
43
|
|
|
console.log("Save token"); |
|
44
|
|
|
localStorage.setItem('jwt', res.data.token); |
|
45
|
|
|
localStorage.setItem('user', this.state.email); |
|
46
|
|
|
console.log(localStorage.getItem('jwt')); |
|
47
|
|
|
|
|
48
|
|
|
window.location.replace("/"); |
|
49
|
|
|
}) |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
}; |
|
53
|
|
|
|
|
54
|
|
|
render() { |
|
55
|
|
|
return ( |
|
56
|
|
|
<div className="login_container"> |
|
57
|
|
|
<form className="form_login" onSubmit={this.handleSubmit}> |
|
58
|
|
|
<label className="input_label"> |
|
59
|
|
|
Email |
|
60
|
|
|
<input |
|
61
|
|
|
required |
|
62
|
|
|
name="email" |
|
63
|
|
|
type="email" |
|
64
|
|
|
onChange={this.handleOnChange} |
|
65
|
|
|
/> |
|
66
|
|
|
</label> |
|
67
|
|
|
|
|
68
|
|
|
<label className="input_label"> |
|
69
|
|
|
Password |
|
70
|
|
|
<input |
|
71
|
|
|
required |
|
72
|
|
|
name="password" |
|
73
|
|
|
type="password" |
|
74
|
|
|
onChange={this.handleOnChange} |
|
75
|
|
|
/> |
|
76
|
|
|
</label> |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
<input name="register" type="Submit" defaultValue="Login" className="button_submit"/> |
|
80
|
|
|
|
|
81
|
|
|
</form> |
|
82
|
|
|
</div> |
|
83
|
|
|
) |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
export default Login; |